package com.woniuxy.microgoldservice.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.woniuxy.bean.ResultBean;
import com.woniuxy.microgoldservice.bean.GoldPriceDTO;
import com.woniuxy.microgoldservice.bean.GoldPriceInfo;
import com.woniuxy.microgoldservice.mapper.GoldAccumulateMapper;
import com.woniuxy.microgoldservice.mapper.GoldPriceMapper;
import com.woniuxy.microgoldservice.service.IGoldAccumulateService;
import com.woniuxy.microgoldservice.service.IGoldpriceService;
import com.woniuxy.microgoldservice.utils.CodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author hongbin
 * @date 2021-04-07 16:12
 * Description：
 */
@RefreshScope
@Api(value = "黄金价格走势图接口",produces = "application/json")
@RequestMapping(value = "/goldPrice",produces = "application/json;charset=utf-8")
@RestController
public class GoldPriceController {


    @Resource
    private IGoldpriceService goldpriceService;

    private final ResultBean MSG = ResultBean.builder().code(Integer.parseInt(CodeEnum.PARAMETER_ERROR.getCode())).msg(CodeEnum.PARAMETER_ERROR.getMessage()).build();

    @ApiOperation(value = "查询历史金价走势",httpMethod = "GET")
    @GetMapping("/selectAll")
    public ResultBean selectAll() {
        return goldpriceService.selectAll();
    }

    @PostMapping("/insert")
    public ResultBean insert(GoldPriceInfo goldPriceInfo) {

        try {
            Double changePercent = goldPriceInfo.getChangePercent();
            Double price = goldPriceInfo.getPrice();
            Double referencePrice = goldPriceInfo.getReferencePrice();
            Long productId = goldPriceInfo.getProductId();
            if (productId == 0.0 || referencePrice==0.0 || price==0.0 ||changePercent==0.0) {
                return MSG;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return MSG;
        }

        int insert = goldpriceService.insert(goldPriceInfo);

        return ResultBean.builder().data(insert).code(200).msg("OK").build();
    }

    @PutMapping("/update")
    public ResultBean update(GoldPriceInfo goldPriceInfo) {
        int insert = goldpriceService.update(goldPriceInfo);
        return ResultBean.builder().data(insert).code(200).msg("OK").build();
    }

    @DeleteMapping("/delete")
    public ResultBean delete(@RequestParam("proId")  Long proId) {
        int insert = goldpriceService.delete(proId);
        return ResultBean.builder().data(insert).code(200).msg("OK").build();
    }



    @ApiOperation(value = "查询走势图所有数据",httpMethod = "GET")
    @GetMapping("/chart")
    public ResultBean selectChart(){
        return goldpriceService.selectChart();
    }


}
